package com.zegome.game.gomestory.main.scene.play.board;

import java.util.Random;
import java.util.Vector;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.zegome.game.action.IAction;
import com.zegome.game.action.IAction.IActionListener;
import com.zegome.game.action.interpolation.SineOutFunction;
import com.zegome.game.entity.IEntity;
import com.zegome.game.entity.action.AlphaAction;
import com.zegome.game.entity.sprite.BaseSprite;

public class LayerMode extends BaseSprite{	
	
	Board board;	
	String data;
	int minX, maxX, minY,maxY;
	int[][] map;
	int total;
	int chapter;
	int col, row;
	Vector<InfoTypePika> infoTypePika = new Vector<InfoTypePika>(); 
	AlphaAction alpha;
	int time;
	
	public void open(){		
		alpha.reset();
		this.registerAction(alpha);				
	}
	
	public LayerMode (final Board board, float pX, float pY) {
		super(pX, pY);
		// TODO Auto-generated constructor stub
		this.board = board;		
		alpha = new AlphaAction(1f, 0.65f, 0.0f, SineOutFunction.getInstance());
		alpha.addActionListener(new IActionListener<IEntity>() {
			
			@Override
			public void onActionStarted (IAction<IEntity> arg0, IEntity arg1) {
				// TODO Auto-generated method stub
			}
			
			
			@Override
			public void onActionFinished (IAction<IEntity> arg0, IEntity arg1) {
				// TODO Auto-generated method stub					
				data = null;
				board.tickTime = 0f;
				board.dataMap.time = time/1000;
				board.dataMap.total = total;
				board.dataMap.totalPikaFix = total;
				board.dataMap.infoTypePika.removeAllElements();
				board.dataMap.infoTypePika.addAll(infoTypePika);				
				
				for(int row=0;row<10;row++){
					for(int col=0;col<14;col++){
						board.dataMap.map[row][col].setPosition(board.dataMap.xBegin+board.dataMap.wi1Pikachu*col,
								board.dataMap.yBegin+board.dataMap.hi1Pikachu*row);			
						board.dataMap.map[row][col].set(map[row][col]);
					}
				}
				board.dataMap.stylePlay = new Random().nextInt(5);
				board.isPlaying = true;
			}
		});
	}
	
	public void setData(int chapter, String data, int row1, int col1){
		//
		//
		this.col = col1;
		this.row = row1;
		//
		this.data = data;		
		//
		map = new int[10][14];
		//
		int total = 0;
		int[][] mapArr = getMap(data,row1,col1);
		int minX = 100, minY = 100, maxX = -1, maxY = -1;
		for(int i=0;i<mapArr.length;i++){
			for(int j = 0; j<mapArr[0].length;j++){
				if(mapArr[i][j] != -1){					
					if(i<minX){
						minX = i;
					}
					if(j<minY){
						minY = j;
					}
					if(i>maxX){
						maxX = i;
					}
					if(j>maxY){
						maxY = j;
					}
				}
				else{
					total ++;
				}
			}
		}		
		//		
		setPosition(board.dataMap.xBegin+board.dataMap.wi1Pikachu*(minY+1), board.dataMap.yBegin+board.dataMap.hi1Pikachu*(minX+1));
		mWidth = board.dataMap.wi1Pikachu*(maxY - minY+1);
		mHeight = board.dataMap.hi1Pikachu*(maxX - minX+1);
		setColor(0.1f, 0.1f, 0.1f, 0.65f);		
		//
		int numberTypePika;
		int stylePlay;
		int maxTypePika = board.screen.ctr.d.maxTypeIcon[chapter];			
				int two = total/2;
				numberTypePika = two-new Random().nextInt(two/2);
				if(numberTypePika>maxTypePika){					
					numberTypePika = maxTypePika;
				}
				time = (int)((two * 3 + numberTypePika * 0.5)*1000);				
				stylePlay = new Random().nextInt(7);
		//		
		Vector<Possition> possitionAble = new Vector<Possition>();
		Vector<Integer> useAblePika = new Vector<Integer>();
		int i,k,rc;
		Random randPika = new Random();
		// Test new icon 
		for(i=0;i<maxTypePika;i++){
			useAblePika.add(i);
		}	
		infoTypePika.removeAllElements();
		for(i=0;i<numberTypePika;i++){			
			k = new Random().nextInt(useAblePika.size());
			infoTypePika.add(new InfoTypePika(useAblePika.elementAt(k), 0));
			useAblePika.removeElementAt(k);
		}
		
		// Second: Prepare map pikaItem from String map
		
		
		for(int row=0;row<10;row++){
			for(int col=0;col<14;col++){
				map[row][col] = -1;
			}
		}
		
		int r,c;
		
		r=1;
		c=1;	
		
		for(i=0;i<data.length();i++){
			if(data.substring(i, i+1).equals("1")){
				possitionAble.add(new Possition(r,c));
			}
			else{
				map[r][c] = -1;
			}
			c++;
			if(c==col1+1){
				c=1;
				r++;
			}
		}		
		// Third: Put in map
		while (possitionAble.size()!=0) {
			
			k = randPika.nextInt(infoTypePika.size());
			rc = new Random().nextInt(possitionAble.size());
			
			map[possitionAble.elementAt(rc).row][possitionAble.elementAt(rc).col] = (infoTypePika.elementAt(k).type);
			possitionAble.removeElementAt(rc);
			
			rc = new Random().nextInt(possitionAble.size());
			map[possitionAble.elementAt(rc).row][possitionAble.elementAt(rc).col] = (infoTypePika.elementAt(k).type);
			possitionAble.removeElementAt(rc);
			
			infoTypePika.elementAt(k).total+=2;
		}			
	}
	
	@Override
	public void onDraw(final SpriteBatch pSpriteBatch, final float pAlpha) {
		if (!isVisible)
			return;
		if(data==null)
			return;
		
		for(int i = 0;i<10;i++){
			for(int j = 0;j<14;j++){
				if(map[i][j] != -1){
					pSpriteBatch.draw(board.screen.ctr.a.squareRegion[board.dataMap.squareNor], board.dataMap.xBegin+j*board.dataMap.wi1Pikachu, board.dataMap.yBegin+i*board.dataMap.hi1Pikachu, board.dataMap.wi1Pikachu, board.dataMap.hi1Pikachu);			
					pSpriteBatch.draw(board.img.pika[map[i][j]], board.dataMap.xBegin+j*board.dataMap.wi1Pikachu, board.dataMap.yBegin+i*board.dataMap.hi1Pikachu, board.dataMap.wi1Pikachu, board.dataMap.hi1Pikachu);			
				}
			}
		}
		
		
//		pSpriteBatch.setColor(mColor.r, mColor.g, mColor.b, mColor.a * pAlpha);
//				
//		if (mScaleX == 1 && mScaleY == 1 && mRotation == 0){
//			pSpriteBatch.draw(board.screen.ctr.a.blackRegion, mX, mY, mWidth, mHeight);			
//		}
//		else {
//			pSpriteBatch.draw(board.screen.ctr.a.blackRegion, mX, mY, mOriginX, mOriginY, mWidth, mHeight,
//				mScaleX, mScaleY, mRotation);
//		}
	}
	
	public int[][] getMap(String s, int row, int col){
		total = 0;
		int[][] map = new int[row][col];
		int r,c;			
		r=0;
		c=0;	
			
		for(int i=0;i<s.length();i++){
			if(s.substring(i, i+1).equals("1")){
				total++;
				map[r][c] = 1;
			}
			else{
				map[r][c] = -1;
			}
			c++;
			if(c==col){
				c=0;
				r++;
			}
		}				
		return map;
	}
}
 